
package com.Model;

import com.Model.Database.Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class CurricularDbAdapter {

    public static final String KEY_NAME = "name";    
    public static final String KEY_DAY = "day";
    public static final String KEY_TIME = "time";    
    public static final String KEY_LOCATION = "location";
    public static final String KEY_COMMENTS = "comments";
    public static final String KEY_ROWID = "_id";
    
    private Database mDbHelper;
    private static SQLiteDatabase mDb;
    private static final String DATABASE_TABLE = "Curricular";
    private final Context context;


    /*
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public CurricularDbAdapter(Context ctx) {
        this.context = ctx;
    }

    /*
     * Open the Curricular database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         Initialisation call)
     * @throws SQLException if the database could be neither opened or created
     */
    public CurricularDbAdapter open() throws SQLException {
        mDbHelper = new Database(context);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }

    
    /*Create a new Extra Curricular Activity using the info provided. If the Activity is
      successfully created return the new rowId for that Activity, otherwise return
      a -1 to indicate failure.
      @return rowId or -1 if failed
     */
    public long createActivity(String name, String day, String time,
    		               String location, String comments) {
        ContentValues initialValues = new ContentValues();
                                 
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_DAY, day);
        initialValues.put(KEY_TIME, time);   
        initialValues.put(KEY_LOCATION, location);
        initialValues.put(KEY_COMMENTS, comments);
        
        //if no Activity Name given then don't add to database
        if(name.equals("")){
        	return -1;
        }
        
        return mDb.insert(DATABASE_TABLE, null, initialValues);
        
    }
  
    /*
     * Delete the Activity with the given rowId
     * 
     * @param rowId id of Activity to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteActivity(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /*
     * Return a Cursor over the list of all Activities in the database
     * 
     * @return Cursor over all Activities
     */
    public Cursor fetchAllActivities() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                KEY_DAY, KEY_TIME,KEY_LOCATION,KEY_COMMENTS}, null, null, null, null, null);
    }

    /*
     * Return a Cursor positioned at the Activity that matches the given rowId
     * 
     * @param rowId id of Activity to retrieve
     * @return Cursor positioned to matching Activity, if found
     * @throws SQLException if Activity could not be found/retrieved
     */
    public static Cursor fetchActivity(long rowId) throws SQLException {

        Cursor mCursor =

                mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                		KEY_DAY, KEY_TIME,KEY_LOCATION,KEY_COMMENTS}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /*
     * Update the Activity using the details provided. The Activity to be updated is
     * specified using the rowId.

     * @param rowId id of Activity to update
     * @param values of Curricular table
     * @return true if the Activity was successfully updated, false otherwise
     */
    public boolean updateActivity(long rowId, String name, String day, String time,
                                  String location, String comments) {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_DAY, day);
        args.put(KEY_TIME, time); 
        args.put(KEY_LOCATION, location);
        args.put(KEY_COMMENTS, comments);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
    
    /*
	  * This method returns the number of Activities in the database.
	  * 
	  * @return The number of Activities currently in the database
	  */
	public int getNumActivities() {
		open();
		// execute the query, which counts the contacts
		Cursor cursor = mDb.query("Curricular", new String[] {"COUNT(name)"}, null, null, null, null, null);
		
		// obtain the data
		int numActivities = 0;
		if (cursor.moveToFirst()) {
			numActivities = cursor.getInt(0);
		}
		cursor.close();
		
		return numActivities;
	}


}



